package com.atkxp.stustatusmasystem.controller;

import com.atkxp.stustatusmasystem.pojo.*;
import com.atkxp.stustatusmasystem.pojo.Class;
import com.atkxp.stustatusmasystem.service.ApplicationService;
import com.atkxp.stustatusmasystem.service.ClassService;
import com.atkxp.stustatusmasystem.service.StudentService;
import com.atkxp.stustatusmasystem.service.TeacherService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author user
 * @declaration
 * @create 2023/5/24-19:55
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    public StudentService studentService;

    @Autowired
    public ApplicationService applicationService;

    @Autowired
    public TeacherService teacherService;

    @Autowired
    public ClassService classService;

    @RequestMapping("toscore")
    public String toScore(String sid, int syear, Model model) {
        List<Course> courses = studentService.getAllCoursesBySidAndSyear(sid,syear);
//        System.out.println(courses);
        model.addAttribute("courses", courses);
        return "score";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest req, Model model) {
        Student login = studentService.login(username, password);
//        System.out.println(login);
        if(login != null) {
            req.getSession().setAttribute("isStudent", true);
            req.getSession().setAttribute("user", login);
            return "redirect:/";
        } else {
            req.getSession().setAttribute("msg", "用户名或者密码错误！");
            return "redirect:/toLogin";
        }
    }

    @RequestMapping("/sendDropApplication")
    @ResponseBody
    public int sendDropApplication(int sid, String msg) {
        Student student = studentService.getStudentById(sid);
        Class clazz = classService.getClassById(student.getClassid());
        DropApplication dropApplication = new DropApplication(0, new Date(), msg, 1, sid, clazz.getTid());
        return applicationService.sendDropApplication(dropApplication);
    }

    @RequestMapping("/sendSwitchApplication")
    @ResponseBody
    public int sendSwitchApplication(int sid, String msg, int classid) {
        Student student = studentService.getStudentById(sid);
        Class clazz = classService.getClassById(student.getClassid());
        SwitchApplication switchApplication = new SwitchApplication(0, new Date(), msg, 1, sid, classid, clazz.getTid());
        return applicationService.sendSwitchApplication(switchApplication);
    }

    @RequestMapping("toSendSwitchApplication")
    public String toSendSwitchApplication(Model model) {
        List<Class> classes = classService.getAllClasses();
        model.addAttribute("classes", classes);
        return "sendSwitchApplication";
    }

    @RequestMapping("toSendDropApplication")
    public String toSendDropApplication() {
        return "sendDropApplication";
    }

    @RequestMapping("toAllSwitchApplication")
    public String toAllSwitchApplication(int sid, Model model) {
        List<SwitchApplication> switchApplications1 = applicationService.getAllSwitchApplicationBySid(sid);
        List<SwitchApp> switchApplications = new LinkedList<>();
        for(SwitchApplication switchApplication : switchApplications1) {
            SwitchApp switchApp = new SwitchApp(switchApplication.getId(), switchApplication.getAppTime(), switchApplication.getMsg(), switchApplication.getStatus(), studentService.getStudentById(switchApplication.getSid())
                    , classService.getClassById(switchApplication.getClassid()), teacherService.getTeacherByTid(switchApplication.getTid()));
            switchApplications.add(switchApp);
        }
        model.addAttribute("switchApplications", switchApplications);
        return "AllSwitchApplication";
    }

    @RequestMapping("toAllDropApplication")
    public String toAllDropApplication(int sid, Model model) {
        List<DropApplication> dropApplications1 = applicationService.getAllDropApplicationBySid(sid);
        List<DropApp> dropApplications = new LinkedList<>();
        for(DropApplication dropApplication : dropApplications1) {
            DropApp dropApp = new DropApp(dropApplication.getId(), dropApplication.getAppTime(), dropApplication.getMsg(), dropApplication.getStatus(),studentService.getStudentById(dropApplication.getSid()), teacherService.getTeacherByTid(dropApplication.getTid()));
            dropApplications.add(dropApp);
        }
        model.addAttribute("dropApplications", dropApplications);
        return "AllDropApplication";
    }

}
